package GUI;

import java.awt.EventQueue;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.KeyStroke;

import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.Label;

public class Login {

	private JFrame frame;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("Login");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setResizable(false);
		
		label();

		passwordField = new JPasswordField();
		passwordField.setBounds(199, 97, 92, 22);
		frame.getContentPane().add(passwordField);

		
		final JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				char[] password = passwordField.getPassword();
				if (isPasswordCorrect(password)) 
				{
					Mainmenu mainframe = new Mainmenu();
					frame.dispose();
				} 
				else 
				{
					JOptionPane.showMessageDialog(frame,
							"Sorry. Wrong Password.", "Error Message",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnLogin.setBounds(194, 132, 97, 25);
		frame.getContentPane().add(btnLogin);
		
		KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
		Action enterAction = new AbstractAction() {
		    // close the frame when the user presses escape
		    public void actionPerformed(ActionEvent e) {
		        btnLogin.doClick();
		    }
		}; 
		frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, "ENTER");
		frame.getRootPane().getActionMap().put("ENTER", enterAction);
	}
	
	private void label()
	{
		Label label = new Label("Password");
		label.setBounds(97, 97, 70, 24);
		frame.getContentPane().add(label);
		
		Label label_1 = new Label("Login");
		label_1.setFont(new Font("Dialog", Font.PLAIN, 40));
		label_1.setBounds(142, 10, 109, 54);
		frame.getContentPane().add(label_1);
	}
	
	private static boolean isPasswordCorrect(char[] inputPassword) 
	{
		char[] actualPassword = { '6', '9', '6', '9' };
		if (inputPassword.length != actualPassword.length)
			return false; // Return false if lengths are unequal
		for (int i = 0; i < inputPassword.length; i++)
			if (inputPassword[i] != actualPassword[i])
				return false;
		return true;
	}
}
